package com.team06.pojo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class TimeFormatUtils {

    public static final DateTimeFormatter UI_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DB_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 将14位时间字符串转换为LocalDateTime
     */
    public static LocalDateTime parse14DigitTime(String timeStr) {
        if (timeStr == null || timeStr.length() != 14 || !timeStr.matches("\\d+")) {
            return null;
        }
        try {
            return LocalDateTime.parse(timeStr, DB_TIME_FORMATTER);
        } catch (DateTimeParseException e) {
            return null;
        }
    }

    /**
     * 将标准日期格式(yyyy-MM-dd)转换为14位开始时间(yyyyMMdd000000)
     */
    public static String convertToDatabaseStartTime(String dateStr) throws DateTimeParseException {
        return LocalDateTime.parse(dateStr + "T00:00:00")
                .format(DB_TIME_FORMATTER);
    }

    /**
     * 将标准日期格式(yyyy-MM-dd)转换为14位结束时间(yyyyMMdd235959)
     */
    public static String convertToDatabaseEndTime(String dateStr) throws DateTimeParseException {
        return LocalDateTime.parse(dateStr + "T23:59:59")
                .format(DB_TIME_FORMATTER);
    }
}
